<? include("conn.php");

error_reporting(0);
session_start();	
$err="";
$correct_ans='';
if(isset($HTTP_SESSION_VARS['attempt']))
	$_SESSION['attempt'] = 0;
if(isset($HTTP_SESSION_VARS['username']))
    unset($HTTP_SESSION_VARS['username']);
if(isset($HTTP_SESSION_VARS['active']))
    unset($HTTP_SESSION_VARS['active']);
 
if(isset($_REQUEST['submit']) && $_REQUEST['submit']=="Login")
{	
	$name = $_REQUEST['username'];
	$pass = $_REQUEST['password'];    
    $qrychk = "SELECT * FROM usermast WHERE user_id='".$name."' and user_pass='".md5($pass)."';";  
	$rschk =mysql_query($qrychk) or die(mysql_error());
	//echo $_SESSION['correct_ans'];
		
	if($row=mysql_fetch_assoc($rschk))
	{
			
		$_SESSION['username']=$row['user_id'];
		$_SESSION['usertype']=$row['usertype'];
		
		header("Location:verify_login.php");
		exit();       
    }
  else
	{
		$attempt_val = intval($_SESSION['attempt']);
		$_SESSION['attempt'] = $attempt_val+1;
		$err = "Invalid username or password!<br>Attempt ".$_SESSION['attempt'];				
	}
}
?>
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6 lt8"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7 lt8"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8 lt8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
        <meta name="author" content="Codrops" />
        <link rel="shortcut icon" href="../favicon.ico"> 
      	<link rel="stylesheet" type="text/css" href="css/style.css" />
		<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
    </head>
  <body>
        <div class="container">
            <header>
            </header>
            <section>	
                <div id="container_demo" >
                    <a class="hiddenanchor" id="toregister"></a>
                    <a class="hiddenanchor" id="tologin"></a>
                    <div id="wrapper">
                        <div id="login" class="animate form">
                            <form  action="index.php" method="post"> 
								<p style="font-size:12px; color:#FF0000; text-align:center;"><?=$err?></p>
                                <h1>Log in</h1> 
                                <p> 
                                    <label for="username" class= "uname" data-icon="u" > Your username </label>
                                    <input type="text" name="username" required="required" />
                                </p>
                                <p> 
                                    <label for="password" class="youpasswd" data-icon="p"> Your password </label>
                                    <input id="password" name="password" required="required" type="password" /> 
                                </p>
								<p>
									<div id="qa">
										<?php
											if(intval($_SESSION['attempt'] > 3))
											{
												$qtemp = "select * from qa_login";
												$restemp = mysql_query($qtemp);
												$numrows = mysql_num_rows($restemp);										
												$idno = rand ( 1 , $numrows );
												echo "<br><label>Please answer the following question:</label><br>";

												$q1 = "SELECT * FROM qa_login WHERE qid='$idno'";  
												$r1 =mysql_query($q1) or die(mysql_error());								
												$row1=mysql_fetch_assoc($r1);
												echo $row1['question'];
												$_SESSION['correct_ans'] = $row1['answer'];
												
												echo '<br><input id="ans" required="required" type="text" class="input-small"/> ';
											}
										?>
									</div>
								</p>
                                <p class="login button"> 
									<input type="submit" id="submit" class="submit" name="submit" value="Login">
								</p>
                            </form>
                        </div>
                    </div>
                </div>  
            </section>
        </div>
    </body>
</html>